package com.payroll.framework.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.log4j.Category;

import com.payroll.framework.Foundation;
import com.payroll.framework.PayrollFoundationException;

/**
 * Provides convenience methods for date functionality
 *
 * @author eWiSACWIS project team
 * @version $Revision:   1.16  $ $Date:   Nov 16 2005 12:11:16  $
 */
public class DateUtility {

	static String dateFormat = "yyyy-MM-dd";
	static final int MAX_DATE_RANGE = 250;

	public static Date calcDate(int numDay, boolean isPast) {
		Date today = new Date();
		long currentTime = today.getTime();

		long millsec = numDay * 24 * 60 * 60 * 1000;
		long targetTime;

		if (isPast)
			targetTime = currentTime - millsec;
		else
			targetTime = currentTime + millsec;

		return new Date(targetTime);

	}
	/**
	 * Converts Timestamp to MM/DD/YYYY HH:MM AM format.
	 * Creation date: (5/5/00 10:37:11 AM)
	 * @return java.lang.String
	 * @param p_Timestamp java.sql.Timestamp
	 */
	public static String convertToUIDateTimeFormatWithAMPM (Timestamp p_timestamp){
		SimpleDateFormat m_format = new SimpleDateFormat("MM/dd/yyyy h:mm a");
		String formattedDate  = "";
		if (p_timestamp!=null) {
			formattedDate = m_format.format(p_timestamp);
		}
		return formattedDate;
	}

	/**
	 * Insert the method's description here.
	 * Creation date: (9/12/00 4:53:55 PM)
	 * @return java.sql.Timestamp
	 * @param numDays int
	 * @param isPast boolean
	 */
	public static java.sql.Timestamp calcDateSQL(int numDays) {
		Date today = new Date();
		long currentTime = today.getTime();

		long millsec = numDays * 24 * 60 * 60 * 1000;
		long targetTime;

		if (numDays < 0)
			targetTime = currentTime - millsec;
		else
			targetTime = currentTime + millsec;

		return new Timestamp(targetTime);

	}
	/**
	 * Insert the method's description here.
	 * Creation date: (12/9/00 11:37:48 AM)
	 * @return java.util.Date
	 * @param argDate java.util.Date
	 */
	public Date convertDatetoSQLDate(Date argDate) {
		long milliSecondsTime = argDate.getTime();
		Timestamp todaysDate = new Timestamp(milliSecondsTime);
		return todaysDate;
	}
	/**
	 * Convert Timestamp to string YYYY-MM-DD
	 * Creation date: (1/11/02 4:35:19 PM)
	 * @return java.lang.String
	 * @param p_Timestamp java.sql.Timestamp
	 */
	public static String convertToString(java.sql.Timestamp p_Timestamp) {
		if (p_Timestamp == null) {
			return null;
		}

		String strDate = (p_Timestamp.toString()).trim();
		if (strDate.length() == 0) {
			return null;
		}

		int year_index = strDate.indexOf("-");
		int month_index = strDate.indexOf("-", year_index + 1);
		int day_index = strDate.indexOf(" ", month_index + 1);

		//Get the year, month, and date.
		String str_year = strDate.substring(0, year_index);
		String str_month = strDate.substring(year_index + 1, month_index);
		String str_date = strDate.substring(month_index + 1, day_index);

		//CONVERTS DATE FORMAT FROM YYYY-MM-DD TO MMDDYYYY
		String dt_modified = str_year + "-" + str_month + "-" + str_date;

		return dt_modified;
	}
	/**
	 * Converts date from YYYY-MM-DD to MM/DD/YYYY format.
	 * Creation date: (5/5/00 10:37:11 AM)
	 * @return java.lang.String
	 * @param argDate java.lang.String
	 */
	public static String convertToUIDateFormat(String argDate) {

		if (argDate == null) {
			return null;
		}

		argDate = argDate.trim();
		if (argDate.length() == 0) {
			return null;
		}

		int year_index = argDate.indexOf("-");
		int month_index = argDate.indexOf("-", year_index + 1);

		//Get the year, month, and date.
		String str_year = argDate.substring(0, year_index);
		String str_month = argDate.substring(year_index + 1, month_index);
		String str_date = argDate.substring(month_index + 1);

		//CONVERTS DATE FORMAT FROM YYYY-MM-DD TO MMDDYYYY
		String dt_modified = str_month + str_date + str_year;

		return dt_modified;
	}
	/**
	 * Converts date from YYYY-MM-DD to MM/DD/YYYY format.
	 * Creation date: (5/5/00 10:37:11 AM)
	 * @return java.lang.String
	 * @param p_Timestamp java.sql.Timestamp
	 */
	public static String convertToUIDateFormat(java.sql.Timestamp p_Timestamp) {

		if (p_Timestamp == null) {
			return null;
		}

		String strDate = (p_Timestamp.toString()).trim();
		if (strDate.length() == 0) {
			return null;
		}

		int year_index = strDate.indexOf("-");
		int month_index = strDate.indexOf("-", year_index + 1);
		int day_index = strDate.indexOf(" ", month_index + 1);

		//Get the year, month, and date.
		String str_year = strDate.substring(0, year_index);
		String str_month = strDate.substring(year_index + 1, month_index);
		String str_date = strDate.substring(month_index + 1, day_index);

		//CONVERTS DATE FORMAT FROM YYYY-MM-DD TO MMDDYYYY
		String dt_modified = str_month + str_date + str_year;

		return dt_modified;
	}
	/**
	 * Converts date from YYYY-MM-DD to MM/DD/YYYY format.
	 * Creation date: (5/5/00 10:37:11 AM)
	 * @return java.lang.String
	 * @param p_Timestamp java.sql.Timestamp
	 */
	public static String convertToUIDateFormatWithSlashes(java.sql.Timestamp p_Timestamp) {

		if (p_Timestamp == null) {
			return null;
		}

		String strDate = (p_Timestamp.toString()).trim();
		if (strDate.length() == 0) {
			return null;
		}

		int year_index = strDate.indexOf("-");
		int month_index = strDate.indexOf("-", year_index + 1);
		int day_index = strDate.indexOf(" ", month_index + 1);

		//Get the year, month, and date.
		String str_year = strDate.substring(0, year_index);
		String str_month = strDate.substring(year_index + 1, month_index);
		String str_date = strDate.substring(month_index + 1, day_index);

		//CONVERTS DATE FORMAT FROM YYYY-MM-DD TO MMDDYYYY
		String dt_modified = str_month + "/" + str_date + "/" + str_year;

		return dt_modified;
	}

	/**
	 * Converts Timestamp to MM/DD/YYYY HH:MM:SS format.
	 * Creation date: (5/5/00 10:37:11 AM)
	 * @return java.lang.String
	 * @param p_Timestamp java.sql.Timestamp
	 */
	public static String convertToUIDateTimeFormatWithSlashes(java.sql.Timestamp p_Timestamp) {

		if (p_Timestamp == null) {
			return null;
		}

		String strDate = (p_Timestamp.toString()).trim();
		if (strDate.length() == 0) {
			return null;
		}

		int year_index = strDate.indexOf("-");
		int month_index = strDate.indexOf("-", year_index + 1);
		int day_index = strDate.indexOf(" ", month_index + 1);
		int time_index = strDate.indexOf(".", day_index + 1);

		//Get the year, month, and date.
		String str_year = strDate.substring(0, year_index);
		String str_month = strDate.substring(year_index + 1, month_index);
		String str_date = strDate.substring(month_index + 1, day_index);
		String m_time = strDate.substring(day_index + 1, time_index);

		//CONVERTS DATE FORMAT FROM YYYY-MM-DD TO MMDDYYYY
		String dt_modified = str_month + "/" + str_date + "/" + str_year + " " + m_time;

		return dt_modified;
	}

	/**
	 * Converts date from YYYY-MM-DD to MM/DD/YYYY format.
	 * Creation date: (5/5/00 10:37:11 AM)
	 * @return java.lang.String
	 * @param p_Timestamp java.sql.Timestamp
	 */
	public static String convertToUIMonthYearFormat(java.sql.Timestamp p_Timestamp) {

		if (p_Timestamp == null) {
			return null;
		}

		String strDate = (p_Timestamp.toString()).trim();
		if (strDate.length() == 0) {
			return null;
		}

		int year_index = strDate.indexOf("-");
		int month_index = strDate.indexOf("-", year_index + 1);
		int day_index = strDate.indexOf(" ", month_index + 1);

		//Get the year, month, and date.
		String str_year = strDate.substring(0, year_index);
		String str_month = strDate.substring(year_index + 1, month_index);
		String str_date = strDate.substring(month_index + 1, day_index);

		//CONVERTS DATE FORMAT FROM YYYY-MM-DD TO MMDDYYYY
		String dt_modified = str_month + "/" + str_year;

		return dt_modified;
	}

	/**
	 * Convert YYYYQ to begin of quarter date.  20011 converts to 01-JAN-2001
	 * Creation date: (9/25/01 3:06:48 PM)
	 * @return java.lang.String
	 */
	public static String convertYearQtrDateToBeginQtrDate(String strYearQuarter) {
		int intQuarter = Integer.parseInt(strYearQuarter.substring(4));

		if (intQuarter == 1)
			return "01-JAN-" + strYearQuarter.substring(0, 4);

		if (intQuarter == 2)
			return "01-APR-" + strYearQuarter.substring(0, 4);

		if (intQuarter == 3)
			return "01-JUL-" + strYearQuarter.substring(0, 4);

		if (intQuarter == 4)
			return "01-OCT-" + strYearQuarter.substring(0, 4);

		return null;
	}
	/**
	 * Argument Dates must be in the format MMDDYYYY and dates must not be null
	 * Creation date: (10/04/00 10:37:11 AM)
	 * @return java.lang.String
	 * @author Subra Honganoor
	 * @param argDate java.lang.String
	 */
	public static boolean dateTwoBeforeDateOne(String dateOne, String dateTwo)
		throws PayrollFoundationException {

		//RETURNS TRUE IF DATE TWO IS BEFORE DATE ONE (DATE ARGUMENT FORMAT: MMDDYYYY)
		//THIS METHOD CAN BE USED TO CHECK IF THE END DATE (dateTwo) IS BEFORE THE EFFECTIVE DATE (dateOne)

		if ((dateOne == null) || (dateTwo == null)) {
			return false;
		}

		int dateOneMM = Integer.parseInt(dateOne.substring(0, 2));
		int dateOneDD = Integer.parseInt(dateOne.substring(2, 4));
		int dateOneYYYY = Integer.parseInt(dateOne.substring(4, 8));

		int dateTwoMM = Integer.parseInt(dateTwo.substring(0, 2));
		int dateTwoDD = Integer.parseInt(dateTwo.substring(2, 4));
		int dateTwoYYYY = Integer.parseInt(dateTwo.substring(4, 8));

		//If date two year is before date one year
		if (dateTwoYYYY < dateOneYYYY) {
			return true;
		}

		//If date two year is after date one year
		if (dateTwoYYYY > dateOneYYYY) {
			return false;
		}

		//If date two year is equal to date one year, check for month
		if (dateTwoYYYY == dateOneYYYY) {

			//If date two month is before date one month
			if (dateTwoMM < dateOneMM) {
				return true;
			}

			//If date two month is after date one month
			if (dateTwoMM > dateOneMM) {
				return false;
			}

			//If date two month is equal to date one month, check for date
			if (dateTwoMM == dateOneMM) {

				//If date two day is before date one day
				if (dateTwoDD < dateOneDD) {
					return true;
				}
				else {
					return false;
				}
			}
		}

		return false;
	}
	/**
	 * This method returns the current date (today's date)
	 * Creation date: (9/13/00 10:00:32 AM)
	 * @return java.sql.Timestamp
	 * @param argDate java.lang.String
	 * @author Subra Honganoor
	 */
	public static Timestamp getCurrentDate() {

		Date currentDate = new Date();

		long milliSecondsTime = currentDate.getTime();

		Timestamp todayDate = new Timestamp(milliSecondsTime);

		return todayDate;
	}
	/**
	*  Returns Quarter
	 */
	public static int getCurrentQuarter() {
		String strYearQtr;
		int intMonth;
		String strCurrentDate;
		// get the current data
		strCurrentDate = getCurrentDate().toString();
		strYearQtr = strCurrentDate.substring(0, 4);

		intMonth = Integer.parseInt(strCurrentDate.substring(5, 2));

		if (intMonth <= 3)
			return 1;

		if (intMonth > 3 && intMonth <= 6)
			return 2;

		if (intMonth > 6 && intMonth <= 9)
			return 3;

		if (intMonth > 9 && intMonth <= 12)
			return 4;

		return -1;
	}
	/**
	 * This method returns the current date (today's date)
	 * Creation date: (9/13/00 10:00:32 AM)
	 * @return java.sql.Timestamp
	 * @param argDate java.lang.String
	 * @author Subra Honganoor
	 *
	 * 2/4/03 snpopovic - cut all overhead by getting mills from System, returning result immed
	 *
	 * 4/29/03 s n popovic - get timestamp from database.  This function should really be throwing a foundation
	 * exception but getCurrentTimestamp doesn't throw this exception from the foundation so al l
	 */
	public static java.sql.Timestamp getCurrentTimestamp() {
		Category cat = Category.getInstance("DateUtility.getCurrentTimestamp");

		Connection m_connection = null;
		PreparedStatement m_ps = null;
		ResultSet m_rs = null;
		java.sql.Timestamp m_timestamp = null;

		try {
			m_connection = Foundation.getConnection();
			m_connection.setAutoCommit(true);

			m_ps = m_connection.prepareStatement("SELECT CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1");
			m_rs = m_ps.executeQuery();

			if(m_rs.next()) {
				m_timestamp = m_rs.getTimestamp(1);
			}
			return m_timestamp;


		}
		catch(SQLException sqe) {
			// do nothing but return the current system timestamp
			return new java.sql.Timestamp(System.currentTimeMillis());
		}
		catch(PayrollFoundationException afe) {
			// do nothing but return the current system timestamp
			return new java.sql.Timestamp(System.currentTimeMillis());
		}
		finally {
			try {
				Foundation.closeJDBCObjects(m_rs,m_ps,m_connection);

			}
			catch (PayrollFoundationException  afe) {
				// do nothing
			}
		}



	}
	/**
	*  Returns Current Year in CCYY format
	 */
	public static String getCurrentYear() {
		// get the current data
		String strCurrentDate = getCurrentDate().toString();
		// return year information
		return strCurrentDate.substring(0, 4);

	}
	/**
	*  Returns Quarter
	 */
	public static String getCurrentYearQuarter() {
		String strYearQtr;
		int intMonth;
		String strCurrentDate;
		// get the current data
		strCurrentDate = getCurrentDate().toString();
		strYearQtr = strCurrentDate.substring(0, 4);

		intMonth = Integer.parseInt(strCurrentDate.substring(5, 7));

		if (intMonth <= 3)
			return strYearQtr + "1";

		if (intMonth > 3 && intMonth <= 6)
			return strYearQtr + "2";

		if (intMonth > 6 && intMonth <= 9)
			return strYearQtr + "3";

		if (intMonth > 9 && intMonth <= 12)
			return strYearQtr + "4";

		return null;
	}

	/**
	*  Returns Current Month
	 */
	public static String getCurrentMonth() {
		// get the current date
		String strCurrentDate = getCurrentDate().toString();
		// return year information
		return strCurrentDate.substring(5, 7);

	}

	/**
	*  Returns Current Month
	* * This method returns the current month in text
	 * Creation date: (12/18/02 15:15)
	 * @return java.util.Date
	 * @param argDate java.lang.String
	 * @author Jessica Lee
	 */
	public static String getCurrentMonthText() {

		int intMonth;
		String strCurrentDate;
		String strCurrentMonth = null;

		// get the current date
		strCurrentDate = getCurrentDate().toString();

		// get the month text and return

		intMonth = Integer.parseInt(strCurrentDate.substring(5, 7));

		switch (intMonth) {
			case 1 :
				strCurrentMonth = "January";
				break;
			case 2 :
				strCurrentMonth = "February";
				break;
			case 3 :
				strCurrentMonth = "March";
				break;
			case 4 :
				strCurrentMonth = "April";
				break;
			case 5 :
				strCurrentMonth = "May";
				break;
			case 6 :
				strCurrentMonth = "June";
				break;
			case 7 :
				strCurrentMonth = "July";
				break;
			case 8 :
				strCurrentMonth = "August";
				break;
			case 9 :
				strCurrentMonth = "September";
				break;
			case 10 :
				strCurrentMonth = "October";
				break;
			case 11 :
				strCurrentMonth = "November";
				break;
			case 12 :
				strCurrentMonth = "December";
				break;
		}
		return strCurrentMonth;
	}

	/**
	*  Returns Text fprmat of the date
	* * This method returns the current month in text
	 * Creation date: (03/09/03 15:15)
	 * @return java.lang.String
	 * @param java.util.Date
	 * @author Sanjay Ramineni
	 */
	public static String getDateInText( java.sql.Timestamp p_Timestamp ) {

		int intMonth;
		String strCurrentDate;
		String strCurrentMonth = null;

		// get the current date
		strCurrentDate = p_Timestamp.toString();

		// get the month text and return

		intMonth = Integer.parseInt(strCurrentDate.substring(5, 7));

		switch (intMonth) {
			case 1 :
				strCurrentMonth = "January";
				break;
			case 2 :
				strCurrentMonth = "February";
				break;
			case 3 :
				strCurrentMonth = "March";
				break;
			case 4 :
				strCurrentMonth = "April";
				break;
			case 5 :
				strCurrentMonth = "May";
				break;
			case 6 :
				strCurrentMonth = "June";
				break;
			case 7 :
				strCurrentMonth = "July";
				break;
			case 8 :
				strCurrentMonth = "August";
				break;
			case 9 :
				strCurrentMonth = "September";
				break;
			case 10 :
				strCurrentMonth = "October";
				break;
			case 11 :
				strCurrentMonth = "November";
				break;
			case 12 :
				strCurrentMonth = "December";
				break;
		}

		// #1949 suppress zero on day
		if (strCurrentDate.substring(8, 9).equalsIgnoreCase("0") ){
			return strCurrentMonth + " " + strCurrentDate.substring(9, 10)+", "+strCurrentDate.substring(0, 4);
		} else {
			return strCurrentMonth + " " + strCurrentDate.substring(8, 10)+", "+strCurrentDate.substring(0, 4);
		}
	}

	/**
	 * This method returns the current date (today's date)
	 * Creation date: (02/08/01 10:00:32 AM)
	 * @return java.util.Date
	 * @param argDate java.lang.String
	 * @author Subra Honganoor
	 */
	public static String getDate30DaysBefore() {

		Category m_cat = Category.getInstance("DateUtility.getDate30DaysBefore");

		String Date30DaysBefore = "";
		String newYear = "";
		String newMonth = "";
		String newDay = "";

		String currentDate = convertToUIDateFormat(getCurrentDate());

		if ((currentDate == null) || (currentDate.length() != 8)) {
			m_cat.debug("Current Date invalid in method getDate30DaysBefore in sys utility");
			return Date30DaysBefore;
		}

		//Get Day, Month, and Year from the current date MMDDYYYY
		String strCurrentMonth = currentDate.substring(0, 2);
		String strCurrentDay = currentDate.substring(2, 4);
		String strCurrentYear = currentDate.substring(4);

		int currentMonth = Integer.parseInt(strCurrentMonth);
		int currentDay = Integer.parseInt(strCurrentDay);
		int currentYear = Integer.parseInt(strCurrentYear);

		// Months having 31 days: 1, 3, 5, 7, 8, 10,12
		// Months having 30 days: 4, 6, 9, 11
		// Months having 28/29 days: 2

		//Calculate New Year
		if (currentMonth == 1 && currentDay < 31) {
			newYear = Integer.toString(currentYear - 1);
		}
		else {
			newYear = strCurrentYear;
		}

		//Calculate New Month
		if ((currentMonth == 1
			|| currentMonth == 3
			|| currentMonth == 5
			|| currentMonth == 7
			|| currentMonth == 8
			|| currentMonth == 10
			|| currentMonth == 12)
			&& currentDay == 31) {
			//30 days before is the 1st of the current month
			newMonth = strCurrentMonth;
			newDay = "01";

			Date30DaysBefore = newYear + newMonth + newDay;
			return Date30DaysBefore;
		}
		else {
			int intNewMonth = currentMonth - 1;

			//Current month is jan and day is less than 31.  So 30 days before is in december previous year
			if (intNewMonth == 0) {
				intNewMonth = 12;
			}

			//Pad month with zeros
			if (intNewMonth < 10) {
				newMonth = "0" + intNewMonth;
			}
			else {
				newMonth = Integer.toString(intNewMonth);
			}
		}

		//Calculate New Day
		int previousMonth = currentMonth - 1;

		//If current month is Jan, previous month is december
		if (previousMonth == 0) {
			previousMonth = 12;
		}

		if (previousMonth == 1
			|| previousMonth == 3
			|| previousMonth == 5
			|| previousMonth == 7
			|| previousMonth == 8
			|| previousMonth == 10
			|| previousMonth == 12) {
			//Previous month has 31 days, so new date is current day + 1
			int intNewDay = currentDay + 1;

			//Pad day with zeros
			if (intNewDay < 10) {
				newDay = "0" + intNewDay;
			}
			else {
				newDay = Integer.toString(intNewDay);
			}

		}
		else {
			newDay = strCurrentDay;
		}

		//If the current month is march and day is less than 3, then new month is Jan
		if ((currentMonth == 3) && (currentDay <= 2)) {

			//New month is January
			newMonth = "01";

			//Assuming Feb has 28 days
			int numberDaysInFeb = 28;
			int intNewDay = (31 - (30 - (numberDaysInFeb + currentDay)));
			newDay = Integer.toString(intNewDay);
		}

		Date30DaysBefore = newMonth + newDay + newYear;

		//Return MMDDYYYY
		return Date30DaysBefore;
	}
	/**
	 * Insert the method's description here.
	 * Creation date: (10/30/00 1:23:28 PM)
	 * @return java.sql.Timestamp
	 * @param days
	 */
	public static java.sql.Timestamp getDateAfter(int days) {
		long q, s, z, x;
		q = days * 24 * 60;
		java.util.Date date = new java.util.Date();
		x = date.getTime();
		s = q * 60 * 1000;
		z = x + s;
		z = getCorrectedTime(z);
		return new java.sql.Timestamp(z);
	}
	/**
	 * Insert the method's description here.
	 * Creation date: (10/30/00 1:23:28 PM)
	 * @return java.sql.Timestamp
	 * @param dyas int
	 */
	public static java.sql.Timestamp getDateAfter(java.sql.Timestamp dtArg, int intDaysAfter) {
		/*
		long lngSeconds = 0;
		long lngDateInMilliseconds = 0;
		long lngTime = 0;
		long lngMinutes = intDaysAfter * 24 * 60;

		lngTime = dtArg.getTime();
		lngSeconds = lngMinutes * 60 * 1000;
		lngDateInMilliseconds = lngTime + lngSeconds;
		lngDateInMilliseconds = getCorrectedTime(lngDateInMilliseconds);

		return new java.sql.Timestamp(lngDateInMilliseconds);
		*/
		Calendar m_calendar = Calendar.getInstance();
		m_calendar.setTime(dtArg);

		m_calendar.add(Calendar.DATE , intDaysAfter);

		return new java.sql.Timestamp(m_calendar.getTime().getTime());
	}

/**
 * This method adds an hour to the time (in milli seconds) if the
 * hour of the day is 23 ( 11:00.00 pm).
 */
	private static long getCorrectedTime(long timeInMilliSecs){
		Calendar m_cal= new GregorianCalendar();
		m_cal.setTime(new Date(timeInMilliSecs));
		if(m_cal.get(Calendar.HOUR_OF_DAY) == 23) {
			if(m_cal.get(Calendar.MINUTE) == 0)
				if(m_cal.get(Calendar.SECOND) == 0)
					return (timeInMilliSecs + 60*60*1000);
		}
		return timeInMilliSecs;
	}


	/**
	 * Insert the method's description here.
	 * Creation date: (10/30/00 1:23:28 PM)
	 * @return java.sql.Timestamp
	 * @param dyas int
	 */
	public static java.sql.Timestamp getDateBefore(java.sql.Timestamp dtArg, int intDaysBefore) {
		long lngSeconds = 0;
		long lngDateInMilliseconds = 0;
		long lngTime = 0;
		long lngMinutes = intDaysBefore * 24 * 60;

		lngTime = dtArg.getTime();
		lngSeconds = lngMinutes * 60 * 1000;
		lngDateInMilliseconds = lngTime - lngSeconds;
		return new java.sql.Timestamp(lngDateInMilliseconds);
	}


	/**
	 * Insert the method's description here.
	 * Creation date: (10/30/00 1:31:38 PM)
	 * @return java.sql.Timestamp
	 * @param days int
	 */
	public java.sql.Timestamp getDateBefore(int days) {
		long q, s, z, x;
		q = days * 24 * 60;
		java.util.Date date = new java.util.Date();
		x = date.getTime();
		s = q * 60 * 1000;
		z = x + s;
		return new java.sql.Timestamp(z);
	}
	/**
	 * This method returns the current date (today's date)
	 * Creation date: (02/02/01 10:00:32 AM)
	 * @return java.util.Date
	 * @param argDate java.lang.String
	 * @author Subra Honganoor
	 */
	public static String getDateFromStringTimestamp(String strTimestamp) {

		//argTimestamp must be in the format 2001-01-31 09:20:51.508
		//return timestampDate will be in the format MMDDYYYY

		String returnTimestampDate = "";

		if (strTimestamp == null) {
			return null;
		}

		int year_index = strTimestamp.indexOf("-");
		int month_index = strTimestamp.indexOf("-", year_index + 1);
		int date_index = strTimestamp.indexOf(" ", month_index + 1);

		//Get the year, month, and date.
		String str_year = strTimestamp.substring(0, year_index);
		String str_month = strTimestamp.substring(year_index + 1, month_index);
		String str_date = strTimestamp.substring(month_index + 1, date_index);

		//CONVERTS TIMESTAMP DATE FORMAT FROM YYYY-MM-DD TO MMDDYYYY
		String dt_modified = str_month + str_date + str_year;

		returnTimestampDate = dt_modified.trim();

		//Return MMDDYYYY
		return returnTimestampDate;
	}
	/**
	 * This method returns the current date (today's date)
	 * Creation date: (02/02/01 10:00:32 AM)
	 * @return java.util.Date
	 * @param argDate java.lang.String
	 * @author Subra Honganoor
	 */
	public static String getDateFromTimestamp(java.sql.Timestamp argTimestamp) {

		//argTimestamp must be in the format 2001-01-31 09:20:51.508
		//return timestampDate will be in the format MMDDYYYY

		String returnTimestampDate = "";

		if (argTimestamp == null) {
			return null;
		}

		String strTimestamp = argTimestamp.toString();

		int year_index = strTimestamp.indexOf("-");
		int month_index = strTimestamp.indexOf("-", year_index + 1);
		int date_index = strTimestamp.indexOf(" ", month_index + 1);

		//Get the year, month, and date.
		String str_year = strTimestamp.substring(0, year_index);
		String str_month = strTimestamp.substring(year_index + 1, month_index);
		String str_date = strTimestamp.substring(month_index + 1, date_index);

		//CONVERTS TIMESTAMP DATE FORMAT FROM YYYY-MM-DD TO MMDDYYYY
		String dt_modified = str_month + str_date + str_year;

		returnTimestampDate = dt_modified.trim();

		//Return MMDDYYYY
		return returnTimestampDate;
	}
	/**
	 * This method returns the current date (today's date)
	 * Creation date: (02/02/01 10:00:32 AM)
	 * @return java.util.Date
	 * @param argDate java.lang.String
	 * @author Subra Honganoor
	 */
	public static String getDateWithSlashesFromTimestamp(String strTimestamp) {

		//argTimestamp must be in the format 2001-01-31 09:20:51.508
		//return timestampDate will be in the format MMDDYYYY

		String returnTimestampDate = "";

		if (strTimestamp == null) {
			return null;
		}

		int year_index = strTimestamp.indexOf("-");
		int month_index = strTimestamp.indexOf("-", year_index + 1);
		int date_index = strTimestamp.indexOf(" ", month_index + 1);

		//Get the year, month, and date.
		String str_year = strTimestamp.substring(0, year_index);
		String str_month = strTimestamp.substring(year_index + 1, month_index);
		String str_date = strTimestamp.substring(month_index + 1, date_index);

		//CONVERTS TIMESTAMP DATE FORMAT FROM YYYY-MM-DD TO MM/DD/YYYY
		String dt_modified = str_month + "/" + str_date + "/" + str_year;

		returnTimestampDate = dt_modified.trim();

		//Return MM/DD/YYYY
		return returnTimestampDate;
	}
	/**
	*  Returns Quarter
	 */
	public static String getPreviousYearQuarter() {
		String strYearQtr;
		String strCurrentDate;
		int intYear = 0;
		int intMonth = 0;
		// get the current data
		strCurrentDate = getCurrentDate().toString();
		intYear = Integer.parseInt(strCurrentDate.substring(0, 4));
		intMonth = Integer.parseInt(strCurrentDate.substring(5, 7));

		if (intMonth <= 3)
			// first quarter moves back to fourth quarter of last year
			return Integer.toString(intYear - 1) + "1";

		if (intMonth > 3 && intMonth <= 6)
			// 2nd quarter moves back to first quarter, same year
			return Integer.toString(intYear) + "1";

		if (intMonth > 6 && intMonth <= 9)
			// 3rd quarter moves back to second quarter, same year
			return Integer.toString(intYear) + "2";

		if (intMonth > 9 && intMonth <= 12)
			// 4th quarter moves back to third quarter, same year
			return Integer.toString(intYear) + "3";

		return null;
	}
	public static boolean isFuture(String aString) throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		Date aDate;

		aDate = formatter.parse(aString);

		Date today = new Date();
		//return true if the argument date is a future date
		return today.before(aDate);
	}
	public static boolean isPast(String aString) throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
		Date aDate;

		aDate = formatter.parse(aString);

		Date today = new Date();

		//The following line uses deprecated methods but up to now we need to
		//take out the time because the time will cause this method to always return
		//true.  That's because the passed in String will always have only the date
		//information and the time will default to 00:00:0000, while today will
		//contain certain time information.
		//today = new Date(today.getYear(), today.getMonth(), today.getDate());

		//return true if the argument date is a past date
		return today.after(aDate);
	}
	/**
	 * Insert the method's description here.
	 * Creation date: (8/27/01 10:56:22 AM)
	 * @param args java.lang.String[]
	 * Scratch function for testing Foundation methods
	 */
	public static void main(String[] args) throws Exception{

		java.text.DateFormat df = java.text.DateFormat.getDateInstance(java.text.DateFormat.SHORT);
		Date dd = df.parse("01/04/03");

		System.out.println("This is the date  : " + dd);

		Timestamp oldTime = new Timestamp(dd.getTime());

		dd = df.parse("05/05/03");

		Calendar m_cal= new GregorianCalendar();
		m_cal.setTime(dd);


		System.out.println("This is the hour of day : " + m_cal.get(Calendar.HOUR_OF_DAY));


		System.out.println("This is the date  : " + dd);
		Timestamp newTime = new Timestamp(dd.getTime());
		/*System.out.println("This is the timestamp  : " + newTime);

		System.out.println("This is the after timestamp  : " + getDateAfter(newTime, 30));

		System.out.println("This is the Year we live  : " + getYear(new Timestamp((new Date()).getTime())));*/

		long m_dateDiffLong = newTime.getTime() - oldTime.getTime();

		long m_divFactorYear = (long) (1000 * 60 * 60 * 24 * 365.25);

		long m_yearDiff = m_dateDiffLong / m_divFactorYear;
		//	long m_monthDiff = m_dateDiffLong/m_divFactorMonth;

		int m_yearDiffInt = (int) m_yearDiff;

		System.out.println("number of years diff : " + calcYearDiff(oldTime, newTime));;
		System.out.println("number of years diff : " + m_yearDiffInt);

		System.out.println("number of days diff : " + calcDaysDiff(oldTime, newTime));

	}
	/**
	 * Insert the method's description here.
	 * Creation date: (10/27/00 10:03:57 AM)
	 * @param TimeStamp java.lang.String
	 */
	public void setTimeStamp(String TimeStamp) {
	}
	/**
	 * Insert the method's description here.
	 * Creation date: (10/27/00 10:09:24 AM)
	 */
	public void setYear() {
	}

	public static boolean isValidTimestamp(Timestamp p_Timestamp) {
		Calendar m_calendar1 = new GregorianCalendar();
		SimpleDateFormat m_simpleDateFormat1 = new SimpleDateFormat(dateFormat);
		Date m_date1;
		try {
			m_date1 = m_simpleDateFormat1.parse(p_Timestamp.toString());
		}
		catch (ParseException e) {
			System.out.println("Exception: " + e.getMessage());
			return false;
		}

		m_calendar1.setTime(m_date1);

		int m_year1 = m_calendar1.get(Calendar.YEAR);

		Timestamp m_timestamp = Foundation.getCurrentTimestamp();

		Calendar m_calendar2 = new GregorianCalendar();
		SimpleDateFormat m_simpleDateFormat2 = new SimpleDateFormat(dateFormat);
		Date m_date2;
		try {
			m_date2 = m_simpleDateFormat2.parse(m_timestamp.toString());
		}
		catch (ParseException e) {
			System.out.println("Exception: " + e.getMessage());
			return false;
		}

		m_calendar2.setTime(m_date2);

		int m_year2 = m_calendar2.get(Calendar.YEAR);

		if (m_year1 < (m_year2 - 125)) {
			return false;
		}

		if (m_year1 > (m_year2 + 125)) {
			return false;
		}

		return true;

	}

	public static boolean isValidTimestampLessThanToday(Timestamp p_Timestamp) {

		Timestamp m_timestamp = Foundation.getCurrentTimestamp();

		if (p_Timestamp.compareTo(m_timestamp) > 0) {
			return false;
		}

		Calendar m_calendar1 = new GregorianCalendar();
		SimpleDateFormat m_simpleDateFormat1 = new SimpleDateFormat(dateFormat);
		Date m_date1;
		try {
			m_date1 = m_simpleDateFormat1.parse(p_Timestamp.toString());
		}
		catch (ParseException e) {
			System.out.println("Exception: " + e.getMessage());
			return false;
		}

		m_calendar1.setTime(m_date1);

		int m_year1 = m_calendar1.get(Calendar.YEAR);

		Calendar m_calendar2 = new GregorianCalendar();
		SimpleDateFormat m_simpleDateFormat2 = new SimpleDateFormat(dateFormat);
		Date m_date2;
		try {
			m_date2 = m_simpleDateFormat2.parse(m_timestamp.toString());
		}
		catch (ParseException e) {
			System.out.println("Exception: " + e.getMessage());
			return false;
		}

		m_calendar2.setTime(m_date2);

		int m_year2 = m_calendar2.get(Calendar.YEAR);

		if (m_year1 < (m_year2 - 125)) {
			return false;
		}

		return true;

	}

	public static boolean isValidTimestampWithin125(Timestamp p_Timestamp) {
		Timestamp m_timestamp = Foundation.getCurrentTimestamp();

		Calendar m_calendar1 = new GregorianCalendar();
		SimpleDateFormat m_simpleDateFormat1 = new SimpleDateFormat(dateFormat);
		Date m_date1;
		try {
			m_date1 = m_simpleDateFormat1.parse(p_Timestamp.toString());
		}
		catch (ParseException e) {
			System.out.println("Exception: " + e.getMessage());
			return false;
		}

		m_calendar1.setTime(m_date1);

		int m_year1 = m_calendar1.get(Calendar.YEAR);

		Calendar m_calendar2 = new GregorianCalendar();
		SimpleDateFormat m_simpleDateFormat2 = new SimpleDateFormat(dateFormat);
		Date m_date2;
		try {
			m_date2 = m_simpleDateFormat2.parse(m_timestamp.toString());
		}
		catch (ParseException e) {
			System.out.println("Exception: " + e.getMessage());
			return false;
		}

		m_calendar2.setTime(m_date2);

		int m_year2 = m_calendar2.get(Calendar.YEAR);

		if ((m_year1 < (m_year2 - 125)) || (m_year1 > (m_year2 + 125))) {
			return false;
		}

		return true;
	}

	public static boolean isValidTimestampWithin125Future(Timestamp p_Timestamp) {
		Timestamp m_timestamp = Foundation.getCurrentTimestamp();

		Calendar m_calendar1 = new GregorianCalendar();
		SimpleDateFormat m_simpleDateFormat1 = new SimpleDateFormat(dateFormat);
		Date m_date1;
		try {
			m_date1 = m_simpleDateFormat1.parse(p_Timestamp.toString());
		}
		catch (ParseException e) {
			System.out.println("Exception: " + e.getMessage());
			return false;
		}

		m_calendar1.setTime(m_date1);

		int m_year1 = m_calendar1.get(Calendar.YEAR);

		Calendar m_calendar2 = new GregorianCalendar();
		SimpleDateFormat m_simpleDateFormat2 = new SimpleDateFormat(dateFormat);
		Date m_date2;
		try {
			m_date2 = m_simpleDateFormat2.parse(m_timestamp.toString());
		}
		catch (ParseException e) {
			System.out.println("Exception: " + e.getMessage());
			return false;
		}

		m_calendar2.setTime(m_date2);

		int m_year2 = m_calendar2.get(Calendar.YEAR);

		if (m_year1 > (m_year2 + 125)) {
			return false;
		}

		return true;
	}

	/**
	 * The method converts the timestamp parameters into <code>Date</code> objects
	 * and compares the <code>Date</code> values. If Timestamp1 is less than
	 * Timestamp2 then a -1 is returned.  If Timestamp1 equals Timestamp2
	 * than a 0 is returned and if Timestamp1 is greater than Timestamp2 than
	 * a 1 is returned.  If either parameter is <code>null</code>, then
	 * that parameter is treated as the earliest date.
	 *
	 * @param p_Timestamp1	the first <code>Timestamp</code> to compare
	 * @param p_Timestamp2	the second <code>Timestamp</code> to compare
	 *
	 * @return -1	if p_Timestamp1 is less than p_Timestamp2, 0 if the parameters
	 * 				are equal, and 1 if p_Timestamp1 is greater than p_Timestamp2
	 *
	 * @throws	PayrollFoundationException if <code>ParseException</code> occurs
	 */
	public static int compareDates(Timestamp p_Timestamp1, Timestamp p_Timestamp2)
		throws PayrollFoundationException {

		// Check for nulls
		if (p_Timestamp1 == null && p_Timestamp2 != null) {
			return -1;
		}
		else if (p_Timestamp1 != null && p_Timestamp2 == null) {
			return 1;
		}
		else if (p_Timestamp1 == null && p_Timestamp2 == null) {
			return 0;
		}

		SimpleDateFormat m_simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

		Date m_date1 = null;
		Date m_date2 = null;

		try {
			m_date1 = m_simpleDateFormat.parse(p_Timestamp1.toString());
			m_date2 = m_simpleDateFormat.parse(p_Timestamp2.toString());
		}
		catch (ParseException pe) {
			throw new PayrollFoundationException("DateUtility.compareDates", pe.getMessage());
		}

		return (m_date1.compareTo(m_date2));
	}

	/**
	 * Convert a MMYYYY or MM/YYYY to a timestamp the result will be YYYY-MM-01 HH:MM:SS
	 * Creation date: (4/12/00 7:10:04 PM)
	 * @return java.sql.Timestamp
	 * S N POPOVIC 4/12/2000
	 */
	public static java.sql.Timestamp convertMMYYYYToTimestamp(String HTMLDate)
		throws PayrollFoundationException {

		String strMonth = null;
		String strDay = "01";
		String strYear = null;

		if (HTMLDate == null) {
			return null;
		}

		HTMLDate = HTMLDate.trim();

		if (HTMLDate.length() == 6) {
			//CONVERTS MMYYYY TO Timestamp
			strMonth = HTMLDate.substring(0, 2);
			strYear = HTMLDate.substring(2);
		}
		else if (HTMLDate.length() == 7) {
			//CONVERTS DATE FORMAT FROM MM/YYYY TO Timestamp
			strMonth = HTMLDate.substring(0, 2);
			strYear = HTMLDate.substring(3);
		}
		else {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		//Convert all subdates to simple ints for comparison purposes
		int intMonth = Integer.parseInt(strMonth);
		int intDay = Integer.parseInt(strDay);
		int intYear = Integer.parseInt(strYear);

		// Month cannot be greater than 12
		if (intMonth > 12 || intMonth <= 0) {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		// Month calculations
		if (intMonth == 1
			|| intMonth == 3
			|| intMonth == 5
			|| intMonth == 7
			|| intMonth == 8
			|| intMonth == 10
			|| intMonth == 12) {
			if (intDay > 31 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}
		}

		if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) {
			if (intDay > 30 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}
		}

		// leap year/feb calculations  (divisible by 4, not by 100 except if divisible by 400)
		if (intMonth == 2) {
			// Feb never has more than 29 Days
			if (intDay > 29 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}

			//
			if (intDay == 29) {
				// leap years divisible by 4
				if (intYear % 4 == 0) {
					// can't be divisible by 100 unless divisible by 400
					if ((intYear % 100 != 0) || (intYear % 100 == 0 && intYear % 400 == 0)) {
						// do nothing - valid leap year
					}
					else {
						throw new PayrollFoundationException(
							"DateUtility.convertHTMLToSQLDate",
							"DateUtility::convertHTMLtoSQLDate");
					}

				}
				else {
					throw new PayrollFoundationException(
						"DateUtility.convertHTMLToSQLDate",
						"DateUtility::convertHTMLtoSQLDate");
				}
			}
		}

		// Year cannot be 0
		if (intYear <= 0) {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		// Convert to JDBC string date
		String strFullDate = strYear + "-" + strMonth + "-" + strDay + " 00:00:00";
		java.sql.Timestamp dtModified = java.sql.Timestamp.valueOf(strFullDate);

		return dtModified;

	}

/**
 * Returns the Difference in years between the timestamp parameters.
 * If either of the parameters is null, then -1 is returned.
 * Also if the first Timestamp (p_Timestamp1) is greater than the
 * second Timestamp (p_Timestamp2) the method returns -1.
 *
 * @author Prakash
 */
	public static int calcYearDiff(Timestamp p_Timestamp1, Timestamp p_Timestamp2)
		throws PayrollFoundationException {
		if (p_Timestamp1 == null) {
			return -1;
		}

		if (p_Timestamp2 == null) {
			return -1;
		}

		long m_dateDiffLong = (p_Timestamp2.getTime() - p_Timestamp1.getTime());

		if (m_dateDiffLong < 0) {
			return -1;
		}

		long m_divFactorYear = (long) (1000 * 60 * 60 * 24 * 365.25);
		long m_yearDiff = m_dateDiffLong / m_divFactorYear;

		return (int) m_yearDiff;
	}


	public static int calcMonthDiff(Timestamp p_Timestamp, Timestamp p_Timestamp1)
		throws PayrollFoundationException {
		if (p_Timestamp == null) {
			return -1;
		}

		if (p_Timestamp1 == null) {
			return -1;
		}

		String strDate1 = (p_Timestamp.toString()).trim();
		if (strDate1.length() == 0) {
			return -1;
		}

		int year_index1 = strDate1.indexOf("-");
		int month_index1 = strDate1.indexOf("-", year_index1 + 1);
		int date_index1 = strDate1.indexOf(" ", month_index1 + 1);

		//Get the year, month, and date.
		String strYear1 = strDate1.substring(0, year_index1);
		String strMonth1 = strDate1.substring(year_index1 + 1, month_index1);
		String strDay1 = strDate1.substring(month_index1 + 1, date_index1);

		int intMonth1 = Integer.parseInt(strMonth1);
		int intDay1 = Integer.parseInt(strDay1);
		int intYear1 = Integer.parseInt(strYear1);

		String strDate2 = (p_Timestamp1.toString()).trim();
		if (strDate2.length() == 0) {
			return -1;
		}

		int year_index2 = strDate2.indexOf("-");
		int month_index2 = strDate2.indexOf("-", year_index2 + 1);
		int date_index2 = strDate2.indexOf(" ", month_index2 + 1);

		//Get the year, month, and date.
		String strYear2 = strDate2.substring(0, year_index2);
		String strMonth2 = strDate2.substring(year_index2 + 1, month_index2);
		String strDay2 = strDate2.substring(month_index2 + 1, date_index2);

		int intMonth2 = Integer.parseInt(strMonth2);
		int intDay2 = Integer.parseInt(strDay2);
		int intYear2 = Integer.parseInt(strYear2);

		Calendar m_calendar1 = new GregorianCalendar();
		Calendar m_calendar2 = new GregorianCalendar();

		m_calendar1.set(intYear1, intMonth1, intDay1);
		m_calendar2.set(intYear2, intMonth2, intDay2);

		long m_date1Long = m_calendar1.getTime().getTime();
		long m_date2Long = m_calendar2.getTime().getTime();

		long m_dateDiffLong = (m_date2Long - m_date1Long);

		if (m_dateDiffLong < 0) {
			return -1;
		}

		//	long m_divFactorYear = (long)(1000*60*60*24*365.25);
		long m_divFactorMonth = (long) (1000 * 60 * 60 * 24 * 30.44);

		//	long m_yearDiff = m_dateDiffLong/m_divFactorYear;
		long m_monthDiff = m_dateDiffLong / m_divFactorMonth;

		//	int m_yearDiffInt = (int)m_yearDiff;
		int m_monthDiffInt = (int) m_monthDiff;

		return m_monthDiffInt;
	}

	public static String getAgeYearMonth(Timestamp p_Timestamp) throws PayrollFoundationException {

		String returnAge = "";

		Timestamp m_timestamp = getCurrentDate();
		int m_yearDiff = calcYearDiff(p_Timestamp, m_timestamp);
		int m_monthDiff = calcMonthDiff(p_Timestamp, m_timestamp);

		if (m_yearDiff == -1) {
			return returnAge;
		}
		else if (m_monthDiff == -1) {
			return returnAge;
		}

		int m_monthDiffInt = (int) m_monthDiff - (m_yearDiff * 12);

		if (String.valueOf(m_yearDiff) == null) {
			returnAge = "";
		}
		else if (m_yearDiff == 0) {
			returnAge = "";
		}
		else if (m_yearDiff == 1) {
			returnAge = String.valueOf(m_yearDiff) + " Year";
		}
		else {
			returnAge = String.valueOf(m_yearDiff) + " Years";
		}

		if (String.valueOf(m_monthDiffInt) == null) {
			returnAge = returnAge;
		}
		else if (m_monthDiffInt == 0) {
			returnAge = returnAge;
		}
		else if (m_monthDiffInt == 1) {
			returnAge = returnAge + " " + String.valueOf(m_monthDiffInt) + " month";
		}
		else {
			returnAge = returnAge + " " + String.valueOf(m_monthDiffInt) + " months";
		}

		return returnAge;
	}
	public static String getCurrentYearYY() {
		// get the current data
		String strCurrentDate = getCurrentDate().toString();
		// return year information
		return strCurrentDate.substring(2, 4);

	}

	/**
	*  Returns YearQuarter for passed in date
	*  Wesley Moore 11/23/2002
	 */
	public static String getYearQuarter(Timestamp p_timestamp) {
		String strYearQtr;
		int intMonth;
		String strDate;
		// get the date
		strDate = p_timestamp.toString();
		strYearQtr = strDate.substring(0, 4);

		intMonth = Integer.parseInt(strDate.substring(5, 7));

		if (intMonth <= 3)
			return strYearQtr + "1";

		if (intMonth > 3 && intMonth <= 6)
			return strYearQtr + "2";

		if (intMonth > 6 && intMonth <= 9)
			return strYearQtr + "3";

		if (intMonth > 9 && intMonth <= 12)
			return strYearQtr + "4";

		return null;
	}

	/**
	*  Returns StartDateForQuarter for passed in date
	*  Wesley Moore 11/23/2002
	 */
	public static Timestamp getStartDateForQuarter(Timestamp p_timestamp) {

		String strMonth = null;
		String strYear = null;
		int intMonth;
		String strDate;
		String strStartDate;
		// get the date
		strDate = p_timestamp.toString();
		strYear = strDate.substring(0, 4);

		intMonth = Integer.parseInt(strDate.substring(5, 7));

		if (intMonth <= 3)
			strMonth = "01";

		if (intMonth > 3 && intMonth <= 6)
			strMonth = "04";

		if (intMonth > 6 && intMonth <= 9)
			strMonth = "07";

		if (intMonth > 9 && intMonth <= 12)
			strMonth = "10";

		if (intMonth > 12 || intMonth < 1)
			strMonth = null;

		if (strMonth != null) {
			String strFullDate = strYear + "-" + strMonth + "-01 00:00:00";
			return java.sql.Timestamp.valueOf(strFullDate);
		}
		else {
			return null;
		}

	}

	/**
	*  Returns EndDateForQuarter for passed in date
	*  Wesley Moore 11/23/2002
	 */
	public static Timestamp getEndDateForQuarter(Timestamp p_timestamp) {

		String strMonth = null;
		String strYear = null;
		String strDay = null;
		int intMonth;
		String strDate;
		String strEndDate;
		// get the date
		strDate = p_timestamp.toString();
		strYear = strDate.substring(0, 4);

		intMonth = Integer.parseInt(strDate.substring(5, 7));

		if (intMonth <= 3) {
			strMonth = "03";
			strDay = "31";
		}

		if (intMonth > 3 && intMonth <= 6) {
			strMonth = "06";
			strDay = "30";
		}

		if (intMonth > 6 && intMonth <= 9) {
			strMonth = "09";
			strDay = "30";
		}

		if (intMonth > 9 && intMonth <= 12) {
			strMonth = "12";
			strDay = "31";
		}

		if (intMonth > 12 || intMonth < 1) {
			strMonth = null;
			strDay = null;
		}

		if (strMonth != null) {
			String strFullDate = strYear + "-" + strMonth + "-" + strDay + " 00:00:00";
			return java.sql.Timestamp.valueOf(strFullDate);
		}
		else {
			return null;
		}

	}

	/**
	*  Returns getFirstDayOfMonth for passed in date
	*  Wesley Moore 11/23/2002
	 */
	public static Timestamp getFirstDayOfMonth(Timestamp p_timestamp) {

		String strMonth = null;
		String strYear = null;
		String strDay = null;
		String strDate;
		String strStartDate;
		// get the date
		strDate = p_timestamp.toString();
		strYear = strDate.substring(0, 4);
		strMonth = strDate.substring(5, 7);
		strDay = "01";

		if (strMonth != null && strYear != null) {
			String strFullDate = strYear + "-" + strMonth + "-" + strDay + " 00:00:00";
			return java.sql.Timestamp.valueOf(strFullDate);
		}
		else {
			return null;
		}

	}

	/**
	*  Returns getLastDayOfMonth for passed in date
	*  Wesley Moore 11/23/2002
	 */
	public static Timestamp getLastDayOfMonth(Timestamp p_timestamp) {

		String strMonth = null;
		String strYear = null;
		String strDay = null;
		int intMonth;
		int intYear;
		String strDate;
		String strStartDate;
		// get the date
		strDate = p_timestamp.toString();
		strYear = strDate.substring(0, 4);
		strMonth = strDate.substring(5, 7);
		intMonth = Integer.parseInt(strDate.substring(5, 7));
		intYear = Integer.parseInt(strDate.substring(0, 4));

		// Month calculations
		if (intMonth == 1
			|| intMonth == 3
			|| intMonth == 5
			|| intMonth == 7
			|| intMonth == 8
			|| intMonth == 10
			|| intMonth == 12) {
			strDay = "31";
		}

		if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) {
			strDay = "30";
		}

		// leap year/feb calculations  (divisible by 4, not by 100 except if divisible by 400)
		if (intMonth == 2) {
			// leap years divisible by 4
			if (intYear % 4 == 0) {
				// can't be divisible by 100 unless divisible by 400
				if ((intYear % 100 != 0) || (intYear % 100 == 0 && intYear % 400 == 0)) {
					strDay = "29";
				}
				else {
					strDay = "28";
				}
			}
			else {
				strDay = "28";
			}
		}

		if (strMonth != null) {
			String strFullDate = strYear + "-" + strMonth + "-" + strDay + " 00:00:00";
			return java.sql.Timestamp.valueOf(strFullDate);
		}
		else {
			return null;
		}

	}

	/**
	*  Returns getStartDateForYear for passed in date
	*  Wesley Moore 11/23/2002
	 */
	public static Timestamp getStartDateForYear(Timestamp p_timestamp) {

		String strMonth = null;
		String strYear = null;
		String strDay = null;
		String strDate;
		String strStartDate;
		// get the date
		strDate = p_timestamp.toString();
		strYear = strDate.substring(0, 4);
		strMonth = "01";
		strDay = "01";

		if (strYear != null) {
			String strFullDate = strYear + "-" + strMonth + "-" + strDay + " 00:00:00";
			return java.sql.Timestamp.valueOf(strFullDate);
		}
		else {
			return null;
		}

	}

	/**
	*  Returns getEndDateForYear for passed in date
	*  Wesley Moore 11/23/2002
	 */
	public static Timestamp getEndDateForYear(Timestamp p_timestamp) {

		String strMonth = null;
		String strYear = null;
		String strDay = null;
		String strDate;
		String strStartDate;
		// get the date
		strDate = p_timestamp.toString();
		strYear = strDate.substring(0, 4);
		strMonth = "12";
		strDay = "31";

		if (strYear != null) {
			String strFullDate = strYear + "-" + strMonth + "-" + strDay + " 00:00:00";
			return java.sql.Timestamp.valueOf(strFullDate);
		}
		else {
			return null;
		}

	}

	public static String convertTimestampToHHMM(Timestamp p_ts) {
		if (p_ts == null) {
			return "";
		}

		String m_time = "";
		SimpleDateFormat m_SimpleDateFormat = new SimpleDateFormat("hh:mm");
		m_SimpleDateFormat.applyPattern("hh:mm");
		m_time = m_SimpleDateFormat.format(p_ts);

		m_SimpleDateFormat.applyPattern("a");
		m_time += m_SimpleDateFormat.format(p_ts);
		return m_time;

	}

	/**
	*  Returns year in CCYY format for passed in date
	*  Wesley Moore 12/25/2002
	 */
	public static int getYear(Timestamp p_timestamp) {

		String strCurrentDate = p_timestamp.toString();
		// return year information
		return Integer.parseInt(strCurrentDate.substring(0, 4));

	}

	/**
	*  Returns month in MM format for passed in date
	*  Wesley Moore 12/25/2002
	 */
	public static int getMonth(Timestamp p_timestamp) {

		String strCurrentDate = p_timestamp.toString();
		// return month information
		return Integer.parseInt(strCurrentDate.substring(5, 7));

	}

	/**
	*  Returns day in DD format for passed in date
	*  Wesley Moore 12/25/2002
	 */
	public static int getDay(Timestamp p_timestamp) {

		String strCurrentDate = p_timestamp.toString();
		// return day information
		return Integer.parseInt(strCurrentDate.substring(8, 10));

	}

	/**
	*  Returns Timestamp of future date based on passed in date and number of months
	*  Wesley Moore 2/4/2003
	*/
	public static Timestamp getDateMonthsInFuture(Timestamp p_timestamp, int p_months) {

		System.out.println(
			"Starting timestamp is " + p_timestamp + " and we are adding " + p_months + " months to it.");

		String strMonth = null;
		String strYear = null;
		String strDay = null;
		int intDay;
		int intMonth;
		int intYear;
		String strDate;
		String strStartDate;

		// get the date
		strDate = p_timestamp.toString();
		strYear = strDate.substring(0, 4);
		strMonth = strDate.substring(5, 7);
		strDay = strDate.substring(8, 10);
		intDay = Integer.parseInt(strDay);
		intMonth = Integer.parseInt(strMonth);
		intYear = Integer.parseInt(strYear);

		if ((intMonth + p_months) > 12) {
			int intAddYears = 0;
			int intAddMonths = 0;
			int intNewMonth = 0;
			int intNewYear = 0;

			if (p_months >= 12) {
				if (p_months == 12) {
					intAddMonths = 0;
					intAddYears = 1;
				}
				else {
					if (p_months % 12 == 0) {
						intAddMonths = 0;
						intAddYears = p_months / 12;
					}
					else {
						intAddMonths = p_months % 12;
						intAddYears = p_months / 12;
					}
				}

				intNewMonth = intMonth + intAddMonths;
				intNewYear = intYear + intAddYears;

			}
			else {
				intNewMonth = intMonth + p_months;
				intAddMonths = intNewMonth % 12;
				intAddYears = intNewMonth / 12;
				intNewYear = intYear + 1;
			}

			String sMonth = String.valueOf(intNewMonth);
			String sYear = String.valueOf(intNewYear);

			String strFullDate = sYear + "-" + sMonth + "-" + strDay + " 00:00:00";
			return java.sql.Timestamp.valueOf(strFullDate);

		}
		else {
			int intNewMonth = intMonth + p_months;
			String sMonth = String.valueOf(intNewMonth);

			String strFullDate = strYear + "-" + sMonth + "-" + strDay + " 00:00:00";
			return java.sql.Timestamp.valueOf(strFullDate);
		}

	}
	/**
	 * Insert the method's description here.
	 * Creation date: (4/12/00 7:10:04 PM)
	 * @return java.util.Date
	 * S N POPOVIC 4/12/2000
	 */
	public static java.sql.Timestamp convertHTMLToSQLDate(String HTMLDate, boolean validationFlag)
		throws PayrollFoundationException {

		if (HTMLDate == null) {
			return null;
		}

		HTMLDate = HTMLDate.trim();
		if (HTMLDate.length() != 8) {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		//CONVERTS DATE FORMAT FROM MMDDYYYY TO DATABASE DATE FORMAT YYYY-MM-DD
		String strMonth = HTMLDate.substring(0, 2);
		String strDay = HTMLDate.substring(2, 4);
		String strYear = HTMLDate.substring(4);

		// return the converted date
		if (validationFlag = false) {
			String strFullDate = strYear + "-" + strMonth + "-" + strDay;
			java.sql.Timestamp dtModified = java.sql.Timestamp.valueOf(strFullDate);
			return dtModified;
		}

		//Convert all subdates to simple ints for comparison purposes
		int intMonth = Integer.parseInt(strMonth);
		int intDay = Integer.parseInt(strDay);
		int intYear = Integer.parseInt(strYear);

		// Month cannot be greater than 12
		if (intMonth > 12 || intMonth <= 0) {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		// Month calculations
		if (intMonth == 1
			|| intMonth == 3
			|| intMonth == 5
			|| intMonth == 7
			|| intMonth == 8
			|| intMonth == 10
			|| intMonth == 12) {
			if (intDay > 31 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}
		}

		if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) {
			if (intDay > 30 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}
		}

		// leap year/feb calculations  (divisible by 4, not by 100 except if divisible by 400)
		if (intMonth == 2) {
			// Feb never has more than 29 Days
			if (intDay > 29 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}

			//
			if (intDay == 29) {
				// leap years divisible by 4
				if (intYear % 4 == 0) {
					// can't be divisible by 100 unless divisible by 400
					if ((intYear % 100 != 0) || (intYear % 100 == 0 && intYear % 400 == 0)) {
						// do nothing - valid leap year
					}
					else {
						throw new PayrollFoundationException(
							"DateUtility.convertHTMLToSQLDate",
							"DateUtility::convertHTMLtoSQLDate");
					}

				}
				else {
					throw new PayrollFoundationException(
						"DateUtility.convertHTMLToSQLDate",
						"DateUtility::convertHTMLtoSQLDate");
				}
			}
		}

		// Year cannot be 0
		if (intYear <= 0) {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		// Convert to JDBC string date
		String strFullDate = strYear + "-" + strMonth + "-" + strDay;
		java.sql.Timestamp dtModified = java.sql.Timestamp.valueOf(strFullDate);

		return dtModified;

	}

	/**
	 * Insert the method's description here.
	 * Creation date: (4/12/00 7:10:04 PM)
	 * @return java.util.Date
	 * S N POPOVIC 4/12/2000
	 */
	public static java.sql.Timestamp convertHTMLToSQLDate(String HTMLDate)
		throws PayrollFoundationException {

		String strMonth = null;
		String strDay = null;
		String strYear = null;

		if (HTMLDate == null) {
			return null;
		}

		HTMLDate = HTMLDate.trim();

		if (HTMLDate.length() == 8) {
			//CONVERTS DATE FORMAT FROM MMDDYYYY TO DATABASE DATE FORMAT YYYY-MM-DD
			strMonth = HTMLDate.substring(0, 2);
			strDay = HTMLDate.substring(2, 4);
			strYear = HTMLDate.substring(4);
		}
		else if (HTMLDate.length() == 10) {
			//CONVERTS DATE FORMAT FROM MM/DD/YYYY TO DATABASE DATE FORMAT YYYY-MM-DD
			strMonth = HTMLDate.substring(0, 2);
			strDay = HTMLDate.substring(3, 5);
			strYear = HTMLDate.substring(6);
		}
		else {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		//Convert all subdates to simple ints for comparison purposes
		int intMonth = Integer.parseInt(strMonth);
		int intDay = Integer.parseInt(strDay);
		int intYear = Integer.parseInt(strYear);

		// Month cannot be greater than 12
		if (intMonth > 12 || intMonth <= 0) {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		// Month calculations
		if (intMonth == 1
			|| intMonth == 3
			|| intMonth == 5
			|| intMonth == 7
			|| intMonth == 8
			|| intMonth == 10
			|| intMonth == 12) {
			if (intDay > 31 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}
		}

		if (intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) {
			if (intDay > 30 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}
		}

		// leap year/feb calculations  (divisible by 4, not by 100 except if divisible by 400)
		if (intMonth == 2) {
			// Feb never has more than 29 Days
			if (intDay > 29 || intDay == 0) {
				throw new PayrollFoundationException(
					"DateUtility.convertHTMLToSQLDate",
					"DateUtility::convertHTMLtoSQLDate");
			}

			//
			if (intDay == 29) {
				// leap years divisible by 4
				if (intYear % 4 == 0) {
					// can't be divisible by 100 unless divisible by 400
					if ((intYear % 100 != 0) || (intYear % 100 == 0 && intYear % 400 == 0)) {
						// do nothing - valid leap year
					}
					else {
						throw new PayrollFoundationException(
							"DateUtility.convertHTMLToSQLDate",
							"DateUtility::convertHTMLtoSQLDate");
					}

				}
				else {
					throw new PayrollFoundationException(
						"DateUtility.convertHTMLToSQLDate",
						"DateUtility::convertHTMLtoSQLDate");
				}
			}
		}

		// Year cannot be 0
		if (intYear <= 0) {
			throw new PayrollFoundationException(
				"DateUtility.convertHTMLToSQLDate",
				"DateUtility::convertHTMLtoSQLDate");
		}

		// Convert to JDBC string date
		String strFullDate = strYear + "-" + strMonth + "-" + strDay + " 00:00:00";
		java.sql.Timestamp dtModified = java.sql.Timestamp.valueOf(strFullDate);

		return dtModified;

	}

	/**
	 * This method returns the passed in timestamp formated string back as a string formated for viewing MM/DD/YYYY
	 * Creation date: (03/03/03 03:33:33 pm)
	 * @return java.util.Date
	 * @param argDate java.lang.String
	 * @author Wesley Moore
	 */
	public static String getUIDateFromStringTimestamp(String strTimestamp) {

		//argTimestamp must be in the format 2001-01-31 09:20:51.508
		//return timestampDate will be in the format MM/DD/YYYY

		String returnTimestampDate = "";

		if (strTimestamp == null) {
			return null;
		}

		int year_index = strTimestamp.indexOf("-");
		int month_index = strTimestamp.indexOf("-", year_index + 1);
		int date_index = strTimestamp.indexOf(" ", month_index + 1);

		//Get the year, month, and date.
		String str_year = strTimestamp.substring(0, year_index);
		String str_month = strTimestamp.substring(year_index + 1, month_index);
		String str_date = strTimestamp.substring(month_index + 1, date_index);

		//CONVERTS TIMESTAMP DATE FORMAT FROM YYYY-MM-DD TO MM/DD/YYYY
		String dt_modified = str_month + "/" + str_date + "/" + str_year;

		returnTimestampDate = dt_modified.trim();

		//Return MM/DD/YYYY
		return returnTimestampDate;

	}


	/**
	 * This method returns the number of days between the timestamps,
	 * that are passed as parameters.
	 *
	 * @return int This is a positive or negative number showing the
	 * 			number of days between the timestamps passed.
	 * @param p_oldTime Timestamp
	 * @param p_newTime Timestamp
	 *
	 * @author Prakash Mandaleeka
	 */
	public static int calcDaysDiff(Timestamp p_oldTime, Timestamp p_newTime) {
		if (p_oldTime == null || p_newTime == null) {
			return -1;
		}
		long m_dateDiffLong = (p_newTime.getTime() - p_oldTime.getTime());
		long m_divFactorDay = (long) (1000 * 60 * 60 * 24);
		int m_numDays = (int)((((double)m_dateDiffLong)/((double)m_divFactorDay)) + 0.5);
		return m_numDays;
	}

	/**
	 * Argument Dates must be in the format MM/DD/YYYY and dates must not be null
	 * Creation date: (10/19/05 )
	 * @return int
	 * @param argDate java.lang.String
	 */

	public static boolean checkConsecutive(String dateOne, String dateTwo)
		throws PayrollFoundationException {

		//RETURNS TRUE IF DATE TWO IS BEFORE DATE ONE (DATE ARGUMENT FORMAT: MM/DD/YYYY)
		//THIS METHOD CAN BE USED TO CHECK IF THE END DATE (dateTwo) IS BEFORE THE EFFECTIVE DATE (dateOne)


		if ((dateOne == null) || (dateTwo == null)) {
			return false;
		}

		Timestamp m_dateOne = null;
		Timestamp m_dateTwo = null;

		m_dateOne = convertHTMLToSQLDate(dateOne);
		m_dateTwo = convertHTMLToSQLDate(dateTwo);

		boolean m_return = false;
		m_return = checkConsecutive(m_dateOne, m_dateTwo);

		return m_return;

	}



	/** Checks to see if the 2 entered timestamps are consecutive. The order of the timestamp parameters does not
	 * matter. It just checks to see if the 2 dates are one after the other.
	 * Author: GV (11/7/2005)
	 * @param p_dateOne
	 * @param p_dateTwo
	 * @return
	 */
	public static boolean checkConsecutive(Timestamp p_dateOne, Timestamp p_dateTwo){
		GregorianCalendar calendar1 = new GregorianCalendar();
		calendar1.setTime(new Date(p_dateOne.getTime()));
		GregorianCalendar calendar2 = new GregorianCalendar();
		calendar2.setTime(new Date(p_dateTwo.getTime()));

		if (calendar1.get(GregorianCalendar.YEAR)==calendar2.get(GregorianCalendar.YEAR)){
			if ( (calendar1.get(GregorianCalendar.DAY_OF_YEAR)-calendar2.get(GregorianCalendar.DAY_OF_YEAR))==1 ||
				 (calendar1.get(GregorianCalendar.DAY_OF_YEAR)-calendar2.get(GregorianCalendar.DAY_OF_YEAR))==-1) {
				return true;
			}
		}else{
			if ( ((calendar1.get(GregorianCalendar.DAY_OF_YEAR)==calendar1.getActualMaximum(GregorianCalendar.DAY_OF_YEAR)) &&
				 (calendar2.get(GregorianCalendar.DAY_OF_YEAR)==calendar2.getActualMinimum(GregorianCalendar.DAY_OF_YEAR))) ||
				 ((calendar1.get(GregorianCalendar.DAY_OF_YEAR)==calendar1.getActualMinimum(GregorianCalendar.DAY_OF_YEAR)) &&
				  (calendar2.get(GregorianCalendar.DAY_OF_YEAR)==calendar2.getActualMaximum(GregorianCalendar.DAY_OF_YEAR)))){
				return true;
			}
		}
		return false;
	}
}